package com.frxuexi.lib_base.http

import com.blankj.utilcode.util.SPUtils
import com.zyj.retrofit.adapter.FlowCallAdapterFactory
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

object RetrofitUtil {

    private val client by lazy {
        OkHttpClient.Builder()
            .connectTimeout(15,TimeUnit.SECONDS)
            .readTimeout(15,TimeUnit.SECONDS)
            .writeTimeout(15,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor{
                val request = it.request().newBuilder()
                    .addHeader("accessId", "29803b1d73124d938f39a46fe59a5d97")
                    .addHeader("token", SPUtils.getInstance().getString(Const.TOKEN,"token"))
                    .build()
                it.proceed(request)
            }
            .build()
    }

    val retrofit by lazy {
        Retrofit.Builder()
            .client(client)
            .baseUrl(Const.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .build()
    }

    private val clientShop by lazy {
        OkHttpClient.Builder()
            .connectTimeout(15,TimeUnit.SECONDS)
            .readTimeout(15,TimeUnit.SECONDS)
            .writeTimeout(15,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor{
                val request = it.request().newBuilder()
                    .addHeader("Authori-zation", "ff80ac4bc6eb452aaa47f93ce16cbfa2")
                    .build()
                it.proceed(request)
            }
            .build()
    }

    val retrofitShop by lazy {
        Retrofit.Builder()
            .client(clientShop)
            .baseUrl(Const.BASE_URL_SHOP)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .build()
    }

    private val clientVideo by lazy {
        OkHttpClient.Builder()
            .connectTimeout(15,TimeUnit.SECONDS)
            .readTimeout(15,TimeUnit.SECONDS)
            .writeTimeout(15,TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .addInterceptor{
                val request = it.request().newBuilder()
                    .addHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJsciIsImV4cCI6MTc2MDIzMzU3M30.ua-OK_9zHg9hKl3OC6odrj5nYcQ9WBThAl84cvxDZFE")
                    .build()
                it.proceed(request)
            }
            .build()
    }

    val retrofitVideo by lazy {
        Retrofit.Builder()
            .client(clientVideo)
            .baseUrl(Const.BASE_URL_VIDEO)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(FlowCallAdapterFactory.create())
            .build()
    }
}